package com.loongtech.bi.manager.player;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.loongtech.bi.entity.player.EntityBackUser7Day;
import com.loongtech.bi.support.chart.ChartData1;
import com.loongtech.bi.support.chart.ChartFlow;
import com.loongtech.bi.support.chart.ChartSelect;
import com.loongtech.core.jpa.manager.ManagerQueryCacheBase;

@Service
public class BackFuncManager extends ManagerQueryCacheBase<EntityBackUser7Day> {
	private static final long serialVersionUID = 1L;

	/**
	 * 获取回流账号数据
	 * 
	 * @param dateStart
	 * @param dateEnd
	 * @param queryPlatform
	 * @return
	 */
	public List<BackUserPlatformData> getBackUserData(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		Map<String, BackUserPlatformData> map = new LinkedHashMap<>();
		// 处理七日回流
		List<Object[]> list1 = getBackUser07Days(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		for (Object[] array : list1) {
			String day = array[0].toString();
			String plat = array[1].toString();
			String chan = array[2].toString();
			String serv = array[3].toString();
			String key = day + "@" + plat + "@" + chan + "@" + serv;
			BackUserPlatformData cur = map.get(key);
			if (null == cur) {
				cur = new BackUserPlatformData();
				cur.setDateStr(day);
				cur.setPlatformId(plat);
				cur.setChannelId(chan);
				cur.setServerId(serv);
				map.put(key, cur);
			}
			cur.setBackuser7day(Integer.valueOf(array[4].toString()));
			cur.setBackrate7day(Double.valueOf(array[5].toString()));
			cur.setBackvipuser7day(Integer.valueOf(array[6].toString()));
			cur.setBackviprate7day(Double.valueOf(array[7].toString()));
		}
		// 处理14日回流
		List<Object[]> list2 = getBackUser14Days(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		for (Object[] array : list2) {
			String day = array[0].toString();
			String plat = array[1].toString();
			String chan = array[2].toString();
			String serv = array[3].toString();
			String key = day + "@" + plat + "@" + chan + "@" + serv;
			BackUserPlatformData cur = map.get(key);
			if (null == cur) {
				cur = new BackUserPlatformData();
				cur.setDateStr(day);
				cur.setPlatformId(plat);
				cur.setChannelId(chan);
				cur.setServerId(serv);
				map.put(key, cur);
			}
			cur.setBackuser14day(Integer.valueOf(array[4].toString()));
			cur.setBackrate14day(Double.valueOf(array[5].toString()));
			cur.setBackvipuser14day(Integer.valueOf(array[6].toString()));
			cur.setBackviprate14day(Double.valueOf(array[7].toString()));
		}
		// 处理30日回流
		List<Object[]> list3 = getBackUser30Days(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		for (Object[] array : list3) {
			String day = array[0].toString();
			String plat = array[1].toString();
			String chan = array[2].toString();
			String serv = array[3].toString();
			String key = day + "@" + plat + "@" + chan + "@" + serv;
			BackUserPlatformData cur = map.get(key);
			if (null == cur) {
				cur = new BackUserPlatformData();
				cur.setDateStr(day);
				cur.setPlatformId(plat);
				cur.setChannelId(chan);
				cur.setServerId(serv);
				map.put(key, cur);
			}
			cur.setBackuser30day(Integer.valueOf(array[4].toString()));
			cur.setBackrate30day(Double.valueOf(array[5].toString()));
			cur.setBackvipuser30day(Integer.valueOf(array[6].toString()));
			cur.setBackviprate30day(Double.valueOf(array[7].toString()));
		}

		return new ArrayList<BackUserPlatformData>(map.values());
	}

	/**
	 * 获取回流设备数据
	 * 
	 * @param dateStart
	 * @param dateEnd
	 * @param queryPlatform
	 * @return
	 */
	public List<BackDevicePlatformData> getBackDeviceData(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		Map<String, BackDevicePlatformData> map = new LinkedHashMap<>();
		List<Object[]> list1 = getBackDevice07Days(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		for (Object[] array : list1) {
			String day = array[0].toString();
			String plat = array[1].toString();
			String chan = array[2].toString();
			String serv = array[3].toString();
			String key = day + "@" + plat + "@" + chan + "@" + serv;
			BackDevicePlatformData cur = map.get(key);
			if (null == cur) {
				cur = new BackDevicePlatformData();
				cur.setDateStr(day);
				cur.setPlatformId(plat);
				cur.setChannelId(chan);
				cur.setServerId(serv);
				map.put(key, cur);
			}
			cur.setMacOldUser7day(Integer.valueOf(array[4].toString()));
			cur.setOldUserMacRate7day(Double.valueOf(array[5].toString()));
			cur.setMacNewUser7day(Integer.valueOf(array[6].toString()));
			cur.setNewUserMacRate7day(Double.valueOf(array[7].toString()));
		}

		List<Object[]> list2 = getBackDevice14Days(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		for (Object[] array : list2) {
			String day = array[0].toString();
			String plat = array[1].toString();
			String chan = array[2].toString();
			String serv = array[3].toString();
			String key = day + "@" + plat + "@" + chan + "@" + serv;
			BackDevicePlatformData cur = map.get(key);
			if (null == cur) {
				cur = new BackDevicePlatformData();
				cur.setDateStr(day);
				cur.setPlatformId(plat);
				cur.setChannelId(chan);
				cur.setServerId(serv);
				map.put(key, cur);
			}
			cur.setMacOldUser14day(Integer.valueOf(array[4].toString()));
			cur.setOldUserMacRate14day(Double.valueOf(array[5].toString()));
			cur.setMacNewUser14day(Integer.valueOf(array[6].toString()));
			cur.setNewUserMacRate14day(Double.valueOf(array[7].toString()));
		}
		List<Object[]> list3 = getBackDevice30Days(dateStart, dateEnd, queryPlatform, queryChannel, queryServer);
		for (Object[] array : list3) {
			String day = array[0].toString();
			String plat = array[1].toString();
			String chan = array[2].toString();
			String serv = array[3].toString();
			String key = day + "@" + plat + "@" + chan + "@" + serv;
			BackDevicePlatformData cur = map.get(key);
			if (null == cur) {
				cur = new BackDevicePlatformData();
				cur.setDateStr(day);
				cur.setPlatformId(plat);
				cur.setChannelId(chan);
				cur.setServerId(serv);
				map.put(key, cur);
			}
			cur.setMacOldUser30day(Integer.valueOf(array[4].toString()));
			cur.setOldUserMacRate30day(Double.valueOf(array[5].toString()));
			cur.setMacNewUser30day(Integer.valueOf(array[6].toString()));
			cur.setNewUserMacRate30day(Double.valueOf(array[7].toString()));
		}
		return new ArrayList<BackDevicePlatformData>(map.values());
	}

	/**
	 * 获取账号7日回流数据
	 * 
	 * @param dateStart
	 * @param dateEnd
	 * @param queryPlatform
	 * @return
	 */
	public List<Object[]> getBackUser07Days(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("select date_format(day,'%Y-%m-%d'),platformid,channelid,serverid,backuser,backrate,backvipuser,backviprate from EntityBackUser7Day  where day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"' order by day desc","dayStart", dateStart, "dayEnd", dateEnd);
	}

	/**
	 * 获取账号14日回流数据
	 * 
	 * @param dateStart
	 * @param dateEnd
	 * @param queryPlatform
	 * @return
	 */
	public List<Object[]> getBackUser14Days(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("select date_format(day,'%Y-%m-%d'),platformid,channelid,serverid,backuser,backrate,backvipuser,backviprate from EntityBackUser14Day  where day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"' order by day desc","dayStart", dateStart, "dayEnd", dateEnd);
	}

	/**
	 * 获取账号30日回流数据
	 * 
	 * @param dateStart
	 * @param dateEnd
	 * @param queryPlatform
	 * @return
	 */
	public List<Object[]> getBackUser30Days(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("select date_format(day,'%Y-%m-%d'),platformid,channelid,serverid,backuser,backrate,backvipuser,backviprate from EntityBackUser30Day  where day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"' order by day desc","dayStart", dateStart, "dayEnd", dateEnd);
	}

	/**
	 * 获取账号7日回流数据
	 * 
	 * @param dateStart
	 * @param dateEnd
	 * @param queryPlatform
	 * @return
	 */
	public List<Object[]> getBackDevice07Days(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("select date_format(day,'%Y-%m-%d'),platformid,channelid,serverid,macOldUser,oldUserMacRate,macNewUser,newUserMacRate from EntityBackDevice7Day  where day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"' order by day desc","dayStart", dateStart, "dayEnd", dateEnd);
	}

	/**
	 * 获取账号14日回流数据
	 * 
	 * @param dateStart
	 * @param dateEnd
	 * @param queryPlatform
	 * @return
	 */
	public List<Object[]> getBackDevice14Days(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("select date_format(day,'%Y-%m-%d'),platformid,channelid,serverid,macOldUser,oldUserMacRate,macNewUser,newUserMacRate from EntityBackDevice14Day  where day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"' order by day desc","dayStart", dateStart, "dayEnd", dateEnd);
	}

	/**
	 * 获取账号30日回流数据
	 * 
	 * @param dateStart
	 * @param dateEnd
	 * @param queryPlatform
	 * @return
	 */
	public List<Object[]> getBackDevice30Days(Date dateStart, Date dateEnd, String queryPlatform, String queryChannel, String queryServer) {
		return findByQuery("select date_format(day,'%Y-%m-%d'),platformid,channelid,serverid,macOldUser,oldUserMacRate,macNewUser,newUserMacRate from EntityBackDevice30Day  where day>=:dayStart and day<=:dayEnd and platformid = '"+queryPlatform+"' and channelid = '"+queryChannel+"' and serverid = '"+queryServer+"' order by day desc","dayStart", dateStart, "dayEnd", dateEnd);
	}

	// 回流设备数据结构
	public class BackDevicePlatformData {
		private String platformId;
		private String channelId;
		private String serverId;
		@ChartFlow(name = "日期")
		private String dateStr; // 日期

		@ChartSelect(name = "7日回流设备", key = 1)
	    @ChartData1(name = "7日回流老账号设备", key = 1)
		private int macOldUser7day=0;// 7日回流老账号设备
		@ChartData1(name = "7日回流新增账号设备", key = 1)
		private int macNewUser7day=0;// 7日回流新增账号设备
		
		@ChartSelect(name = "7日回流设备占比", key = 2)
	    @ChartData1(name = "7日回流老账号设备占比", key = 2)
		private double oldUserMacRate7day=0.0d;// 7日回流老账号设备占比
		@ChartData1(name = "7日回流新增账号设备占比", key = 2)
		private double newUserMacRate7day=0.0d;// 7日回流新增账号设备占比

		@ChartSelect(name = "14日回流设备", key = 3)
	    @ChartData1(name = "14日回流老账号设备", key = 3)
		private int macOldUser14day=0;// 14日回流老账号设备
		@ChartData1(name = "14日回流新增账号设备", key = 3)
		private int macNewUser14day=0;// 14日回流新增账号设备
		
		@ChartSelect(name = "14日回流设备占比", key = 4)
	    @ChartData1(name = "14日回流老账号设备占比", key = 4)
		private double oldUserMacRate14day=0.0d;// 14日回流老账号设备占比
		@ChartData1(name = "14日回流新增账号设备占比", key = 4)
		private double newUserMacRate14day=0.0d;// 14日回流新增账号设备占比

		@ChartSelect(name = "30日回流设备", key = 5)
	    @ChartData1(name = "30日回流老账号设备", key = 5)
		private int macOldUser30day=0;// 30日回流老账号设备
		@ChartData1(name = "30日回流新增账号设备", key = 5)
		private int macNewUser30day=0;// 30日回流新增账号设备
		
		@ChartSelect(name = "30日回流设备占比", key = 6)
	    @ChartData1(name = "30日回流老账号设备占比", key = 6)
		private double oldUserMacRate30day=0.0d;// 30日回流老账号设备占比
		@ChartData1(name = "30日回流新增账号设备占比", key = 6)
		private double newUserMacRate30day=0.0d;// 30日回流新增账号设备占比

		public String getPlatformId() {
			return platformId;
		}

		public void setPlatformId(String platformId) {
			this.platformId = platformId;
		}
		
		public String getChannelId() {
			return channelId;
		}

		public void setChannelId(String channelId) {
			this.channelId = channelId;
		}

		public String getServerId() {
			return serverId;
		}

		public void setServerId(String serverId) {
			this.serverId = serverId;
		}

		public String getDateStr() {
			return dateStr;
		}

		public void setDateStr(String dateStr) {
			this.dateStr = dateStr;
		}

		public int getMacOldUser7day() {
			return macOldUser7day;
		}

		public void setMacOldUser7day(int macOldUser7day) {
			this.macOldUser7day = macOldUser7day;
		}

		public double getOldUserMacRate7day() {
			return oldUserMacRate7day;
		}

		public void setOldUserMacRate7day(double oldUserMacRate7day) {
			this.oldUserMacRate7day = oldUserMacRate7day;
		}

		public int getMacNewUser7day() {
			return macNewUser7day;
		}

		public void setMacNewUser7day(int macNewUser7day) {
			this.macNewUser7day = macNewUser7day;
		}

		public double getNewUserMacRate7day() {
			return newUserMacRate7day;
		}

		public void setNewUserMacRate7day(double newUserMacRate7day) {
			this.newUserMacRate7day = newUserMacRate7day;
		}

		public int getMacOldUser14day() {
			return macOldUser14day;
		}

		public void setMacOldUser14day(int macOldUser14day) {
			this.macOldUser14day = macOldUser14day;
		}

		public double getOldUserMacRate14day() {
			return oldUserMacRate14day;
		}

		public void setOldUserMacRate14day(double oldUserMacRate14day) {
			this.oldUserMacRate14day = oldUserMacRate14day;
		}

		public int getMacNewUser14day() {
			return macNewUser14day;
		}

		public void setMacNewUser14day(int macNewUser14day) {
			this.macNewUser14day = macNewUser14day;
		}

		public double getNewUserMacRate14day() {
			return newUserMacRate14day;
		}

		public void setNewUserMacRate14day(double newUserMacRate14day) {
			this.newUserMacRate14day = newUserMacRate14day;
		}

		public int getMacOldUser30day() {
			return macOldUser30day;
		}

		public void setMacOldUser30day(int macOldUser30day) {
			this.macOldUser30day = macOldUser30day;
		}

		public double getOldUserMacRate30day() {
			return oldUserMacRate30day;
		}

		public void setOldUserMacRate30day(double oldUserMacRate30day) {
			this.oldUserMacRate30day = oldUserMacRate30day;
		}

		public int getMacNewUser30day() {
			return macNewUser30day;
		}

		public void setMacNewUser30day(int macNewUser30day) {
			this.macNewUser30day = macNewUser30day;
		}

		public double getNewUserMacRate30day() {
			return newUserMacRate30day;
		}

		public void setNewUserMacRate30day(double newUserMacRate30day) {
			this.newUserMacRate30day = newUserMacRate30day;
		}
	}

	// 回流账号数据结构
	public class BackUserPlatformData {
		private String platformId;
		private String channelId;
		private String serverId;
		@ChartFlow(name = "日期")
		private String dateStr=""; // 日期
		@ChartSelect(name = "7日回流用户", key = 1)
	    @ChartData1(name = "7日回流用户数", key = 1)
		private int backuser7day=0; // 7日回流用户数
        @ChartData1(name = "7日回流VIP用户数", key = 1)
		private int backvipuser7day=0; // 7日回流VIP用户数
        
        @ChartSelect(name = "7日回流用户占比", key = 2)
	    @ChartData1(name = "7日回流用户占比", key = 2)
		private double backrate7day=0.0d; // 7日回流用户占比
        @ChartData1(name = "7日回流VIP用户占比", key = 2)
		private double backviprate7day=0.0d; // 7日回流VIP用户占比

        @ChartSelect(name = "14日回流用户", key = 3)
	    @ChartData1(name = "14日回流用户数", key = 3)
		private int backuser14day=0; // 14日回流用户数
        @ChartData1(name = "14日回流VIP用户数", key = 3)
		private int backvipuser14day=0; // 14日回流VIP用户数
        
        @ChartSelect(name = "14日回流用户占比", key = 4)
	    @ChartData1(name = "14日回流用户占比", key = 4)
		private double backrate14day=0.0d; // 14日回流用户占比
        @ChartData1(name = "14日回流VIP用户占比", key = 4)
		private double backviprate14day=0.0d; // 14日回流VIP用户占比
		
        @ChartSelect(name = "30日回流用户", key = 5)
	    @ChartData1(name = "30日回流用户数", key = 5)
		private int backuser30day=0; // 30日回流用户数
        @ChartData1(name = "30日回流VIP用户数", key = 5)
		private int backvipuser30day=0; // 30日回流VIP用户数
		
        @ChartSelect(name = "30日回流用户占比", key = 6)
	    @ChartData1(name = "30日回流用户占比", key = 6)
		private double backrate30day=0.0d; // 30日回流用户占比
        @ChartData1(name = "30日回流VIP用户占比", key = 6)
		private double backviprate30day=0.0d; // 30日回流VIP用户占比

		public String getPlatformId() {
			return platformId;
		}

		public void setPlatformId(String platformId) {
			this.platformId = platformId;
		}
		
		public String getChannelId() {
			return channelId;
		}

		public void setChannelId(String channelId) {
			this.channelId = channelId;
		}

		public String getServerId() {
			return serverId;
		}

		public void setServerId(String serverId) {
			this.serverId = serverId;
		}

		public String getDateStr() {
			return dateStr;
		}

		public void setDateStr(String dateStr) {
			this.dateStr = dateStr;
		}

		public int getBackuser7day() {
			return backuser7day;
		}

		public void setBackuser7day(int backuser7day) {
			this.backuser7day = backuser7day;
		}

		public double getBackrate7day() {
			return backrate7day;
		}

		public void setBackrate7day(double backrate7day) {
			this.backrate7day = backrate7day;
		}

		public int getBackvipuser7day() {
			return backvipuser7day;
		}

		public void setBackvipuser7day(int backvipuser7day) {
			this.backvipuser7day = backvipuser7day;
		}

		public double getBackviprate7day() {
			return backviprate7day;
		}

		public void setBackviprate7day(double backviprate7day) {
			this.backviprate7day = backviprate7day;
		}

		public int getBackuser14day() {
			return backuser14day;
		}

		public void setBackuser14day(int backuser14day) {
			this.backuser14day = backuser14day;
		}

		public double getBackrate14day() {
			return backrate14day;
		}

		public void setBackrate14day(double backrate14day) {
			this.backrate14day = backrate14day;
		}

		public int getBackvipuser14day() {
			return backvipuser14day;
		}

		public void setBackvipuser14day(int backvipuser14day) {
			this.backvipuser14day = backvipuser14day;
		}

		public double getBackviprate14day() {
			return backviprate14day;
		}

		public void setBackviprate14day(double backviprate14day) {
			this.backviprate14day = backviprate14day;
		}

		public int getBackuser30day() {
			return backuser30day;
		}

		public void setBackuser30day(int backuser30day) {
			this.backuser30day = backuser30day;
		}

		public double getBackrate30day() {
			return backrate30day;
		}

		public void setBackrate30day(double backrate30day) {
			this.backrate30day = backrate30day;
		}

		public int getBackvipuser30day() {
			return backvipuser30day;
		}

		public void setBackvipuser30day(int backvipuser30day) {
			this.backvipuser30day = backvipuser30day;
		}

		public double getBackviprate30day() {
			return backviprate30day;
		}

		public void setBackviprate30day(double backviprate30day) {
			this.backviprate30day = backviprate30day;
		}
	}
}